package com.ev.asl.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ev.asl.domain.AslReportWorkDO;
import com.ev.asl.dto.AslReportWorkParam;
import com.ev.asl.mapper.AslReportWorkMapper;
import com.ev.asl.service.AslReportWorkService;
import com.ev.asl.vo.AslReportWorkVo;
import com.ev.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AslReportWorkServiceImpl  extends ServiceImpl<AslReportWorkMapper, AslReportWorkDO>
        implements AslReportWorkService {

    @Autowired
    private AslReportWorkMapper aslReportWorkMapper;

    @Autowired
    private UserService userService;

    @Override
    public Object pageList(AslReportWorkParam param) {
        //分页
        Page<AslReportWorkDO> page = new Page<AslReportWorkDO>(param.getPageNo(), param.getPageSize());
        IPage<AslReportWorkDO> vo = aslReportWorkMapper.pageList(page, param);
        for (AslReportWorkDO record : vo.getRecords()) {
            if(record.getUserId() != null){
                String[] userIds = record.getUserId().split(",");
                StringBuilder sb = new StringBuilder();
                for (String userId : userIds) {
                    if(sb.length() > 0){
                        sb.append(",");
                    }
                    sb.append(userService.get(Long.valueOf(userId)).getName());
                }
                record.setUserName(sb.toString());
            }
        }
        return vo;
    }

    @Override
    public AslReportWorkVo getByshift(Integer shift, Long mpiId, Date startTime, Date endTime) {
        return aslReportWorkMapper.getByshift(shift,mpiId,startTime,endTime);
    }
}
